﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using mamnontuoitho7.AppCode;
using System.IO;

namespace mamnontuoitho7.backend
{
    public partial class ArticleList : System.Web.UI.Page
    {
        private ArticleType _articleType = new ArticleType();
        private Article _article = new Article();
        private Product _product = new Product();
        private Member _mem = new Member();
        protected DataTable _dt;

        protected void Page_Load(object sender, EventArgs e)
        {

            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionArticle).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                if (Request.Form["hidProductID"] != null && Request.Form["hidVisible"] != null)
                {
                    try
                    {
                        string _ProductID = Request.Form["hidProductID"].ToString();
                        string _Status = Request.Form["hidVisible"].ToString();
                        _product.ChangeProductStatus(_ProductID, _Status);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Response.Redirect(ConstantClass._errorAdminPage);
                    }
                    Response.Redirect("../backend/ArticleList.aspx");
                }

                if (Request.Form["hidProductIDDelete"] != null && Request.Form["hidPhotoDelete"] != null)
                {
                    try
                    {
                        string _ProductID = Request.Form["hidProductIDDelete"].ToString();
                        string _Photo = Request.Form["hidPhotoDelete"].ToString();

                        /// delete image and thumbnail
                        string localfile = Request.PhysicalApplicationPath + "images/article/" + _Photo;
                        string localfilethumb = Request.PhysicalApplicationPath + "images/article/thumbnail/" + _Photo;
                        if (File.Exists(localfile))
                        {
                            File.Delete(localfile);
                            File.Delete(localfilethumb);
                        }

                        _product.ProductDelete(_ProductID);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Response.Redirect(ConstantClass._errorAdminPage);
                    }
                    Response.Redirect("../backend/ArticleList.aspx");
                }
                try
                {
                    if (!IsPostBack)
                    {
                        // load drop down list article type
                        DataTable dtArticleType = _articleType.GetAllArticleType2();
                        ddlArticleType.DataSource = dtArticleType;
                        ddlArticleType.DataTextField = "ArticleTypeName";
                        ddlArticleType.DataValueField = "ArticleTypeID";
                        ddlArticleType.DataBind();
                        //add select dropdownlist
                        ddlArticleType.Items.Insert(0, new ListItem("Xem Tất Cả", "0"));

                        ddlAmountRecord.Items.Insert(0, new ListItem("50 Bài Viết Mới Nhất", "50"));
                        ddlAmountRecord.Items.Insert(1, new ListItem("Tất Cả Bài Viết", "0"));

                        if (Request.Cookies["ArticleList_keepStatusDDLArticleType"] == null)
                        {
                            Response.Cookies["ArticleList_keepStatusDDLArticleType"].Value = "0";
                            Response.Cookies["ArticleList_keepStatusDDLArticleType"].Expires = DateTime.Now.AddDays(1);
                        }
                        _dt = _article.GetArticleByType(Request.Cookies["ArticleList_keepStatusDDLArticleType"].Value, ddlAmountRecord.SelectedValue);
                        ddlArticleType.SelectedValue = Request.Cookies["ArticleList_keepStatusDDLArticleType"].Value;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }

            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void ddlArticleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _dt = _article.GetArticleByType(ddlArticleType.SelectedValue, ddlAmountRecord.SelectedValue);
                Response.Cookies["ArticleList_keepStatusDDLArticleType"].Value = ddlArticleType.SelectedValue;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }

        protected void ddlAmountRecord_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _dt = _article.GetArticleByType(ddlArticleType.SelectedValue, ddlAmountRecord.SelectedValue);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }

    }
}